from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Page
from pyecharts.render import make_snapshot
# from snapshot_phantomjs import snapshot
from snapshot_selenium import snapshot


def do_zu_he_tu(arr_label, value_1, title_1, value_2, title_2, value_3, title_3, js_path, title_name=''):
    cBar = (
        Bar(init_opts=opts.InitOpts(width='750px', height='350px', js_host=js_path))
        .add_xaxis(arr_label)
        .add_yaxis(title_1, value_1, label_opts=opts.LabelOpts(is_show=True, position='top', formatter="{c}"))
        .add_yaxis(title_2, value_2, label_opts=opts.LabelOpts(is_show=True, position='top', formatter="{c}"))
        # .extend_axis(
        #     yaxis=opts.AxisOpts()
        # )
        .set_global_opts(title_opts=opts.TitleOpts(title=title_name, pos_left='center'),
                         legend_opts=opts.LegendOpts(is_show=True, orient='horizontal', pos_top="bottom"),
                         xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=False)),
                         yaxis_opts=opts.AxisOpts(axistick_opts=opts.AxisTickOpts(is_show=True),
                                                  splitline_opts=opts.SplitLineOpts(is_show=False)))
    )
    cLine = (
        Line()
        .add_xaxis(arr_label)
        .add_yaxis(title_3, value_3,
                   label_opts=opts.LabelOpts(is_show=True, position='top', formatter="{@[1]}", color='#28527a'))
    )
    cBar.overlap(cLine)
    return cBar


def do_zhu_tu(arr_label, value_1, title_1, value_2, title_2, js_path='', title_name=''):
    cBar = (
        Bar(init_opts=opts.InitOpts(width='750px', height='350px', js_host=js_path))
        .add_xaxis(arr_label)
        .add_yaxis(title_1, value_1, label_opts=opts.LabelOpts(is_show=True, position='top', formatter="{c}"))
        .add_yaxis(title_2, value_2, label_opts=opts.LabelOpts(is_show=True, position='top', formatter="{c}"))
        .set_global_opts(title_opts=opts.TitleOpts(title=title_name, pos_left='center'),
                         xaxis_opts=opts.AxisOpts(axislabel_opts={"interval": "0"},
                                                  splitline_opts=opts.SplitLineOpts(is_show=False)),
                         legend_opts=opts.LegendOpts(is_show=True, orient='horizontal', pos_top="bottom"),
                         yaxis_opts=opts.AxisOpts(axistick_opts=opts.AxisTickOpts(is_show=True),
                                                  splitline_opts=opts.SplitLineOpts(is_show=False)))
    )
    return cBar


def do_line_tu(arr_label, value_1, title_1, value_2, title_2, value_3, title_3, js_path, title_name=''):
    cLine = (
        Line(init_opts=opts.InitOpts(width='750px', height='350px', js_host=js_path))
        .add_xaxis(arr_label)
        .add_yaxis(title_1, value_1, color='#007FFFCC',
                   label_opts=opts.LabelOpts(is_show=True, position='top', font_size=14, formatter="{@[1]}"),
                   linestyle_opts=opts.LineStyleOpts(width=3))
        .add_yaxis(title_2, value_2, color='#FF993399',
                   label_opts=opts.LabelOpts(is_show=True, position='top', font_size=14, formatter="{@[1]}"),
                   linestyle_opts=opts.LineStyleOpts(width=3))
        .add_yaxis(title_3, value_3, color='#66339970',
                   label_opts=opts.LabelOpts(is_show=True, position='top', font_size=14, formatter="{@[1]}"),
                   linestyle_opts=opts.LineStyleOpts(width=3))
        # .add_yaxis(title_1, value_1, color='#4169E1',
        #            label_opts=opts.LabelOpts(is_show=True, position='top', font_size=14, formatter="{@[1]}"),
        #            linestyle_opts=opts.LineStyleOpts(width=2))
        # .add_yaxis(title_2, value_2, color='#FF8C00',
        #            label_opts=opts.LabelOpts(is_show=True, position='top', font_size=14, formatter="{@[1]}"),
        #            linestyle_opts=opts.LineStyleOpts(width=2))
        # .add_yaxis(title_3, value_3, color='#28527a',
        #            label_opts=opts.LabelOpts(is_show=True, position='top', font_size=14, formatter="{@[1]}"),
        #            linestyle_opts=opts.LineStyleOpts(width=2))
        # .add_yaxis(title_3, value_3, color='#28527a',
        #            label_opts=opts.LabelOpts(is_show=True, position='top', font_size=14, formatter="{@[1]}"),
        #            linestyle_opts=opts.LineStyleOpts(width=2, type_="dashed"))
        .set_global_opts(title_opts=opts.TitleOpts(title=title_name, pos_left='center'),
                         legend_opts=opts.LegendOpts(is_show=True, orient='horizontal', pos_top="bottom"),
                         xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=False)),
                         yaxis_opts=opts.AxisOpts(axistick_opts=opts.AxisTickOpts(is_show=True),
                                                  splitline_opts=opts.SplitLineOpts(is_show=False)))
    )
    return cLine


def do_lines_tu(arr_label, list_title, map_value, js_path, title_name=''):
    cLine = (
        Line(init_opts=opts.InitOpts(width='750px', height='350px', js_host=js_path))
        .add_xaxis(arr_label)
        .set_global_opts(title_opts=opts.TitleOpts(title=title_name, pos_left='center'),
                         legend_opts=opts.LegendOpts(is_show=True, orient='horizontal', pos_top="bottom"),
                         xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=False)),
                         yaxis_opts=opts.AxisOpts(axistick_opts=opts.AxisTickOpts(is_show=True),
                                                  splitline_opts=opts.SplitLineOpts(is_show=False)))
    )
    # rows = []
    for index in list_title:
        cLine.add_yaxis(index, map_value[index],
                        label_opts=opts.LabelOpts(is_show=True, position='top', font_size=14, formatter="{@[1]}"),
                        linestyle_opts=opts.LineStyleOpts(width=2))
        # rows.append(map_value[index])
    # # 添加表格到折线图中
    # Bar().set_global_opts().extend_axis()
    # table = cLine.set_global_opts().ex
    #     extend_series([{'type': 'line', 'data': [('A', 50), ('B', 30), ('C', 20)]}], series_name='Line')
    # # 创建页面并设置布局为上图下表
    # page = Page().layout(opts.PageLayoutOpts(pos_left=None))
    # page.use_theme("dark").add(cLine).add(table)
    # # 生成 HTML 文件
    # page.render("chart_with_table.html")
    # 添加标题、网格等
    cLine.set_global_opts(title_opts=opts.TitleOpts(title='Line Chart'))
    cLine.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
    cLine.set_global_opts(toolbox_opts=opts.ToolboxOpts())
    cLine.extend_axis(yaxis=opts.AxisOpts(name='Y-Value'))
    return cLine


def do_zu_he_tu_image(arr_label, value1, title1, value2, title2, value3, title3, image_name, js_path='',
                      title_name=''):
    make_snapshot(snapshot, do_zu_he_tu(arr_label, value1, title1, value2, title2, value3, title3, js_path,
                                        title_name).render('render.html'), image_name)


def do_zhu_tu_image(arr_label, value1, title1, value2, title2, image_name, js_path='', title_name=''):
    make_snapshot(snapshot, do_zhu_tu(arr_label, value1, title1, value2, title2, js_path, title_name).render('render.html'),
                  image_name)


def do_line_tu_image(arr_label, value1, title1, value2, title2, value3, title3, image_name, js_path='', title_name=''):
    make_snapshot(snapshot, do_line_tu(arr_label, value1, title1, value2, title2, value3, title3, js_path, title_name).render('render.html'),
                  image_name)


def do_lines_tu_image(arr_label, list_title, map_value, image_name, js_path='', title_name=''):
    make_snapshot(snapshot, do_lines_tu(arr_label, list_title, map_value, js_path, title_name).render('render.html'),
                  image_name)


# # 1、准备数据
# tuOneLabels = ['12月28日', '12月29日', '12月30日', '12月31日', '1月1日', '1月2日', '1月3日']
# listBuDaBiaoKeShi = ['兴辰佳苑临时党支部', '北关东路社区']
# listBuDaBiaoKeShiJianMian = {'兴辰佳苑临时党支部': [None, 100, 0, None, 72, 0, 50], '北关东路社区': [None, 100, None, None, None, None, 50]}
# strTuBuDaBiaoKeShiTitle = '七日内机关科室办结工单见面率情况'
# fileImageBuDaBiaoKeShiSave = 'E:\\日常工作\\数据组日常工作-韩冲\\生成每日必办\\bar0.png'
# render_name = 'E:\\日常工作\\数据组日常工作-韩冲\\生成每日必办\\pyecharts_assets\\'
# do_lines_tu_image(tuOneLabels, listBuDaBiaoKeShi, listBuDaBiaoKeShiJianMian, fileImageBuDaBiaoKeShiSave,
#                                  render_name, strTuBuDaBiaoKeShiTitle)
